
Imports System
Imports System.Collections

Namespace _2bMatesSite.BO
	''' <summary>
	''' Represents a list that is able to return a random item, and a list of random items
	''' </summary>
	Public MustInherit Class RandomizedList
		' NOTE: needs to be initialized by the inheriting class
		Protected rnd As Random


		' Get a random item out of the list
		Public Function RandomItem() As Object
			Return RandomItem(GetList())
		End Function

		' Get a set of random items of the list
		Public Function GetRandomItems(ByVal cnt As Integer) As ArrayList
			Dim nonPickedItems As New ArrayList(GetList())
			Dim rndList As New ArrayList()

			cnt = Math.Min(cnt, GetList().Count)

			Dim added As Integer = 0
			While added < cnt
				Dim item As Object = RandomItem(nonPickedItems)

				nonPickedItems.Remove(item)
				rndList.Add(item)
				System.Math.Max(System.Threading.Interlocked.Increment(added),added - 1)
			End While

			Return rndList
		End Function

		' Get a random item off the given list
		Protected Function RandomItem(ByVal list As IList) As Object
			Dim rndItem As Object = Nothing
			If list.Count > 0 Then
				rndItem = list(rnd.[Next](list.Count))
			End If

			Return rndItem
		End Function

		Public MustOverride Function GetList() As IList
	End Class
End Namespace






